<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Cache;

class Chart extends Base
{
    /**
     * 曲线图
     * 优先读取缓存
     */
    public function chartLine(){
        $data = Cache::get('data_chartLine');
        if (!$data) {
            $c = model('customer')->chartLine();
            $o = model('order')->chartLine();
            $d = model('customer')->chartLineD();

            $data = array_merge_recursive($c,$o, $d);
            Cache::set('data_chartLine', $data,3600);
        }
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
    }

    /**
     * 首页统计参数判断
     * @param $date
     */
    public function count($date)
    {
        if(empty($date)) {
            $this->countNoParam();
        } else {
            $this->countWithParam($date);
        }
    }

    /**
     * 首页整体统计（不包含参数）
     * 优先读取缓存
     */
    protected function countNoParam(){
        $data = Cache::get('data_count');

        if (!$data) {
            $c = model('customer')->countId();
            $o = model('order')->countId();
            $d = model('customer')->countD();

            $data1 = array_column($c , 'brand');
            $data2 = array_column($c , 'num');
            $data3 = array_column($o , 'num');
            $data4 = array_column($d , 'num');

            foreach($data1 as $key=>$val){
                $data[$key]['品牌']=$data1[$key];
                $data[$key]['售卡数']=$data2[$key];
                $data[$key]['订单数']=$data3[$key];
                $data[$key]['带单数']=$data4[$key];
            }
            Cache::set('data_count', $data,3600);
        }

        echo json_encode($data, JSON_UNESCAPED_UNICODE);
    }

    /**
     * 首页分日统计（包含日期参数）
     * @param $date
     */
    protected function countWithParam($date){
        $c = model('customer')->dateCount($date);
        $o = model('order')->dateCount($date);
        $d = model('customer')->dateCountD($date);

        $data1 = array_column($c , 'brand');
        $data2 = array_column($c , 'num');
        $data3 = array_column($o , 'num');
        $data4 = array_column($d , 'num');

        foreach($data1 as $key=>$val){
            $data[$key]['品牌']=$data1[$key];
            $data[$key]['售卡数']=$data2[$key];
            $data[$key]['订单数']=$data3[$key];
            $data[$key]['带单数']=$data4[$key];
        }
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
    }

}
